package com.keepc.excel.controller;

import com.keepc.excel.service.DataImportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;

@RestController
@Slf4j
public class DataImportController {

    @Autowired
    private DataImportService dataImportService;

    /**
     * 导入数据 http://localhost:8080/import
     */
    @GetMapping("/import")
    public String importData() {
        log.info("导入数据开始");
        // 读取resources目录下的文件
        String exportExcelFileName = "import_user.xlsx";
        ClassPathResource resource = new ClassPathResource("file/" + exportExcelFileName);
        try {
            InputStream inputStream = resource.getInputStream();
            dataImportService.importData(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "数据导入成功";
    }
}
